const express = require('express')
const router =express.Router()

const Follow = require('../models/follow')
const UserModel = require('../models/user')
const BlogModel = require('../models/blog')
const Collection = require('../models/collection')
const Nice = require('../models/nice')

router.get('/findFollow',(req,res,next)=>{
  const {user_id} = req.query
  Follow.find({user_id:user_id}).then(result => {
      const followeds = result.map(item => item.followed_id)
      UserModel.find().then(result => {
        res.json({
          code:200,
          data:result.filter(it =>{
            if(followeds.some(current=>current==it.user_id))return it
          }),
          message:'查询完成',
      })
      })
  })
})

router.get('/findFans',(req,res,next)=>{
  const {user_id} = req.query
  Follow.find({followed_id:user_id}).then(result => {
      const fans = result.map(item => item.user_id)
      UserModel.find().then(result => {
        res.json({
          code:200,
          data:result.filter(it =>{
            if(fans.some(current=>current==it.user_id))return it
          }),
          message:'查询完成',
      })
      })
  })
})

router.post('/followCreate',(req,res,next)=>{
  const {user_id,followed_id} = req.body
  new Follow({
      user_id,
      followed_id,
  }).save().then(result => {
      res.json({
          code:200,
          data:result,
          message:'创建完成',
      })
  })
})

router.delete('/followDelete',(req,res,next)=>{
  const {user_id,followed_id} = req.body

  Follow.deleteOne({user_id,followed_id}).then(result=>{
      res.json({
          code:200,
          data:{
              result
          },
          message:'关注删除完成',
      })
  })
})

router.get('/findNice',(req,res,next)=>{
  const {blog_id} = req.query
  Nice.find({blog_id:blog_id}).then(result => {
      res.json({
        code:200,
        data:result,
        message:'查询完成',
    })
  })
})

router.post('/niceCreate',(req,res,next)=>{
  const {user_id,blog_id} = req.body
  new Nice({
      user_id,
      blog_id,
  }).save().then(result => {
      res.json({
          code:200,
          data:result,
          message:'创建完成',
      })
  })
})

router.delete('/niceDelete',(req,res,next)=>{
  const {user_id,blog_id} = req.body

  Nice.deleteOne({user_id,blog_id}).then(result=>{
      res.json({
          code:200,
          data:{
              result
          },
          message:'点赞删除完成',
      })
  })
})

router.get('/findCollection',(req,res,next)=>{
  const {user_id} = req.query
  Collection.find({user_id:user_id}).then(result => {
    const collections = result.map(item => item.blog_id)
    BlogModel.aggregate([
      {
        $match: {} 
      },
      {
        $lookup: {
          from: 'users',
          localField: 'user_id',
          foreignField: 'user_id',
          as: 'user_data',
        }
      },
      {
        $lookup: {
          from: 'comments',
          localField: 'blog_id',
          foreignField: 'blog_id',
          as: 'comment_data',
        }
      },
      {
        $lookup: {
          from: 'collections',
          localField: 'blog_id',
          foreignField: 'blog_id',
          as: 'collections_data',
        }
      },
      {
        $lookup: {
          from: 'nices',
          localField: 'blog_id',
          foreignField: 'blog_id',
          as: 'nices_data',
        }
      },
      {
        $lookup: {
          from: 'follows',
          localField: 'user_id',
          foreignField: 'followed_id',
          as: 'follows_data',
        }
      },
    ]).then(result => {
        res.json({
          code:200,
          data:result.filter(it =>{
            if(collections.some(current=>current==it.blog_id))return it
          }),
          message:'查询完成',
      })
      })
  })
})

router.post('/collectionCreate',(req,res,next)=>{
  const {user_id,blog_id} = req.body
  new Collection({
      user_id,
      blog_id,
  }).save().then(result => {
      res.json({
          code:200,
          data:result,
          message:'创建完成',
      })
  })
})

router.delete('/collectionDelete',(req,res,next)=>{
  const {user_id,blog_id} = req.body

  Collection.deleteOne({user_id,blog_id}).then(result=>{
      res.json({
          code:200,
          data:{
              result
          },
          message:'收藏删除完成',
      })
  })
})
module.exports = router